package com.xjrsoft.common.runner;

import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.enums.IotProtocolEnum;
import com.xjrsoft.common.utils.IotUtil;
import com.xjrsoft.common.utils.RedisUtil;
import com.xjrsoft.module.iot.entity.IotDic;
import com.xjrsoft.module.iot.entity.IotLink;
import com.xjrsoft.module.iot.service.IIotDicService;
import com.xjrsoft.module.iot.service.IIotGroupService;
import com.xjrsoft.module.iot.service.IIotLinkService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: tzx
 * @Date: 2024/4/9 0:06
 */
@Component
@AllArgsConstructor
@Slf4j
public class IotRunner implements CommandLineRunner {

    private RedisUtil redisUtil;
    private final IIotLinkService iotLinkServicel;

    private final IIotDicService iotDicService;

    @Override
    public void run(String... args) throws Exception {
        List<IotDic> dicList = iotDicService.list();

        redisUtil.set(GlobalConstant.IOT_DIC_CACHE_KEY,dicList);
        List<IotLink> list = iotLinkServicel.list();

        for (IotLink iotLink : list) {
            if (iotLink.getProtocolType() == IotProtocolEnum.MQTT.getCode()) {

//                List<IotDic> linkDicList = dicList.stream().filter(dic -> dic.getLinkId().equals(iotLink.getId())).collect(Collectors.toList());
                IotUtil.initMqttServer(iotLink);
            }
            else {
//                List<IotDic> linkDicList = dicList.stream().filter(dic -> dic.getLinkId().equals(iotLink.getId())).collect(Collectors.toList());
                IotUtil.initOpcuaServer(iotLink);
            }
        }


    }



}
